package Server.ServerCore.Controller;


import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Repository.ChipsServiceImpl;
import Server.ServerCore.Service.DataPacks.packRequest.Bet_requst;
import Server.ServerCore.Service.DataPacks.packRespose.Bet_response;
import Server.ServerCore.Service.DataPacks.packRespose.Chips_reback_Response;
import Server.ServerCore.Service.DataPacks.packRespose.bust_response;
import Server.ServerCore.Service.DataPacks.packRespose.cardBean_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import Server.ServerCore.Service.usyns.SingelTableManager;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-1
 *         Time: 下午9:55
 *         BetController =21 下注处理
 *
 *         ok
 */
@Service
public class BetController extends HandleController {

    private static final long serialVersionUID = 7613458695475724854L;
    @Resource
    private ChipsServiceImpl chipsService;
    @Resource
    private SingelTableManager singelTableManager;

    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
        //牌局开始标记
        player.setOver(false);
        player.setBet(true);
        PlayerManagerService.clear(player);

        Bet_requst betRequst= (Bet_requst) parseObject(buffer,Bet_requst.class);
        int[] list=betRequst.getList();//0,1,2,3,4（从右向左）
        CardResultStatusVO[] tempCardStatusVO=new CardResultStatusVO[12];

        int chipsTemp=0;
        for (int circleUnitID=0;circleUnitID!=list.length;circleUnitID++){
            if (list[circleUnitID]!=0){//下注信息以及牌信息保存bean CardResultStatusVO,i== 0,1,2,3,4（从右向左）
                CardResultStatusVO cardResultStatusVO=new CardResultStatusVO();
                cardResultStatusVO.setChipsPoints(list[circleUnitID]);//设定筹码点数
                cardResultStatusVO.setCircleUnitID(circleUnitID);//位置 对应扑克牌的位置
                tempCardStatusVO[circleUnitID]=cardResultStatusVO;
                player.getCircleUnitIDS().add(circleUnitID);//保存已经下注的位置对应-->CircleUnitID
                chipsTemp+=list[circleUnitID];
                player.atomicInteger.incrementAndGet();//+1
            }
        }



        Collections.sort(player.getCircleUnitIDS());//标记下注的位置
        player.setDeckoOfCardsVOFastList(tempCardStatusVO);
        tempCardStatusVO=null;

        //发牌(初始化)--注意是针对已经下注的CircleUnit，从右向左顺序0,1,2,3,4，5（5:bankerAI card,0--4:player card）
        FastList<cardBean_response> cards_list=new FastList<cardBean_response>();
        boolean bet=false;//庄家没有发牌
        //针对已经下注的玩家CircleUnit发牌
        for (int i=0;i<2;i++){//发两张牌
            for (Integer CircleUnitID:player.getCircleUnitIDS()){

//                if (CircleUnitID==0){//test
//                   for (;;){
//                       CardVO cardVO=new CardVO();
//                       if (cardVO.getCardType().getID()==6){
//                           cardBean_response cardBeanResponse=new cardBean_response();
//                           cardBeanResponse.setCardType(cardVO.getCardType().getID());
//                           cardBeanResponse.setCardcolour(cardVO.getCardcolour());
//                           cardBeanResponse.setCircleUnitID(CircleUnitID);
//                           cardVO.setCircleUnitID(CircleUnitID);
//                           player.getCardVOFastList().add(cardVO);
//                           cards_list.add(cardBeanResponse);
//                           break;
//                       }
//                   }
//                }else {
                    CardVO cardVO=new CardVO();
                    cardBean_response cardBeanResponse=new cardBean_response();
                    cardBeanResponse.setCardType(cardVO.getCardType().getID());
                    cardBeanResponse.setCardcolour(cardVO.getCardcolour());
                    cardBeanResponse.setCircleUnitID(CircleUnitID);
                    cardVO.setCircleUnitID(CircleUnitID);
                    player.getCardVOFastList().add(cardVO);
                    cards_list.add(cardBeanResponse);
//                }
            }
            //庄家牌
            if (!bet){
//                for (;;){//test
//                    CardVO card=new CardVO();
//                    if (card.getCardType().getName()=="A"){
//                        card.setCircleUnitID(5);
//                        //获取玩家所在牌桌的庄家
//                        singelTableManager.getBankerAI(player).getCardVOFastList().add(card);
//                        cardBean_response cardResponse=new cardBean_response();
//                        cardResponse.setCardType(card.getCardType().getID());
//                        cardResponse.setCardcolour(card.getCardcolour());
//                        cardResponse.setCircleUnitID(5);
//                        cards_list.add(cardResponse);
//                        bet=true;
//                        break;
//                    }
//                }
                CardVO card=new CardVO();
                card.setCircleUnitID(5);
                //获取玩家所在牌桌的庄家
                singelTableManager.getBankerAI(player).getCardVOFastList().add(card);
                cardBean_response cardResponse=new cardBean_response();
                cardResponse.setCardType(card.getCardType().getID());
                cardResponse.setCardcolour(card.getCardcolour());
                cardResponse.setCircleUnitID(5);
                cards_list.add(cardResponse);
                bet=true;
            }
           }


            //断开模拟发包
            if (chipsTemp>player.getBargainingChip()){
                player.getPlayerSession().getChannel().close();
                PlayerManagerService.clear(player);
            }

            //推送数据 21
            Bet_response betResponse=new Bet_response();
            betResponse.setCards_list(cards_list);
            player.DirectSend(betResponse.getJsonData(2,1));
            cards_list.clear();


            //返回玩家筹码余额
            double lastchips=player.getBargainingChip()-chipsTemp;
            player.setBargainingChip(lastchips);


            //返回筹码额
            Chips_reback_Response rebackResponse=new Chips_reback_Response();
            rebackResponse.setChips(player.getBargainingChip());
            player.DirectSend(rebackResponse.getJsonData(2,7));

            chipsService.updateChips(player.getGameRole(),player.getBargainingChip());



            //返回nextcircleUnitID   最右边位置
            bust_response bustResponse=new bust_response();
            int now=player.getCircleUnitIDS().get(0);
            bustResponse.setNextcircleUnitID(now);
            player.setNowCrileUnitID(now);
            player.DirectSend(bustResponse.getJsonData(2,9));



    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[21]=this;
    }



    public static void main(String[]args){
        List<Integer> temp=new FastList<Integer>();
        temp.add(1);
        temp.add(9);
        temp.add(2);
        temp.add(5);
        Collections.sort(temp);
        for (int i:temp){
            System.out.println(i);
        }

    }
}
